﻿using Module_Debug.Hardware;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace Module_Debug.VDDIC_Gamma
{
    public partial class RM692E5_ADB : Form
    {

        #region 参数定义
        ADB_Other Adbcmd = new ADB_Other();
        CaSDK CaSDKObj = new CaSDK();
        CRC16 CRCData = new CRC16();
        Gamma_Addr GAMMAs = new Gamma_Addr();
        //Class_JOJO_gamma Flexible = new Class_JOJO_gamma();
        private bool Listening = false;//是否没有执行完invoke相关操作   
        private bool IClosing = false;//是否正在关闭串口，执行Application.DoEvents，并阻止再次invoke 

        private List<byte> buffer = new List<byte>(2048);//默认分配1/16页内存，并始终限制不允许超过
        private double[,] D_lums;//目标值-单色
        private double[] D_lum;//目标值-白色
        private double lum_off, x_off, y_off;
        private int index = 0;
        private int flag1 = 1;
        private int flag2 = 1;
        private int flag3 = 1;
        private byte[] Send_buf = new byte[1];
        private byte[] Rec_tmp = new byte[1];
        //创建类型为Person的对象集合
        List<Setting.Gamma_Property> Diven_Gamma = new List<Setting.Gamma_Property>();
        List<Setting.Offset_Property> Diven_Offset = new List<Setting.Offset_Property>();
        private static bool Gamma_adjust_timeout_flag = false;

        private byte[] gamma_r = new byte[54]
        {
                0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,0x1a,0x1b,0x1c,0x1d,
                0x1e,0x1f,0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29,0x2a,0x2b,0x2d,0x2f,0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37
        };
        private byte[] gamma_g = new byte[54]
        {
                0x48,0x49,0x4a,0x4b,0x4c,0x4d,0x4e,0x4f,0x50,0x51,0x52,0x53,0x54,0x55,0x56,0x58,0x59,0x5a,0x5b,0x5c,0x5d,0x5e,0x5f,0x60,0x61,0x62,0x63,0x64,0x65,0x66,
                0x67,0x68,0x69,0x6a,0x6b,0x6c,0x6d,0x6e,0x6f,0x70,0x71,0x72,0x73,0x74,0x75,0x76,0x77,0x78,0x79,0x7a,0x7b,0x7c,0x7d,0x7e
        };
        private byte[] gamma_b = new byte[54]
        {
                0x8d,0x8e,0x8f,0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9a,0x9b,0x9c,0x9d,0x9e,0x9f,0xa0,0xa2,0xa3,0xa4,0xa5,0xa6,0xa7,0xa9,0xaa,0xab,0xac,
                0xad,0xae,0xaf,0xb0,0xb1,0xb2,0xb3,0xb4,0xb5,0xb6,0xb7,0xb8,0xb9,0xba,0xbb,0xbc,0xbd,0xbe,0xbf,0xc0,0xc1,0xc2,0xc3,0xc4
        };

        private int[,] R_HZ = new int[30, 17];
        private int[,] G_HZ = new int[30, 17];
        private int[,] B_HZ = new int[30, 17];

        byte R_MSB_REG = 0;
        byte G_MSB_REG = 0;
        byte B_MSB_REG = 0;

        //Gamma绑点定义
        // private byte[] Point_RM692EX = new byte[33] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32 };
        private int Total_points = 0;
        private int Frame_selected = 1;
        private int Gamma_selected = 1;
        private byte Gamma_selected_value = 1;

        #region 窗体ID
        //RM692EX 监听挂载
        frmMain frmHelp = null;
        public RM692E5_ADB(frmMain RM692E5_ADB)
        {
            frmHelp = RM692E5_ADB;
            InitializeComponent();
            frmHelp.Child_window_hide_already = true;
            frmHelp.Child_Gamma_Window_Open_Enable = false;//防止重复开启子窗体    
            frmHelp.Hide();

        }
        //RM692EX 监听卸载
        private void RM692EX_FormClosed(object sender, FormClosedEventArgs e)
        {
            frmHelp.Child_window_hide_already = false;
            frmHelp.Child_Gamma_Window_Open_Enable = true; //允许其他子窗体打开
            textBox1.Text = textBox1.Text + "RM692EX_FormClosed！\r\n";
            frmHelp.IC_comboBox.Enabled = true;
            Environment.Exit(0);
        }

        private void RM692EX_Gamma_VisibleChanged(object sender, EventArgs e)
        {
            if (frmHelp.Child_window_hide_already == false) //子窗体已开启标识
            {
                textBox1.Text = textBox1.Text + "RM692EX_FormHide！\r\n";
                frmHelp.Child_window_hide_already = true; //子窗体已隐藏标识
            }
            else
            {
                textBox1.Text = textBox1.Text + "RM692EX_FormOpened！\r\n";
                frmHelp.Child_window_hide_already = false; //子窗体已开启标识
            }
        }
        #endregion

        #region GAMMA保存和载入
        //Gamma保存
        private void RM692EX_SaveGamma_Click(object sender, EventArgs e)
        {
            SaveFileDialog sf = new SaveFileDialog(); //对话框
            sf.Filter = @"csv文件(*.csv)|*.csv|所有文件(*.*)|*.*"; //定义保存的文件的类型
            string str = "保存Gamma文件";
            if (sf.ShowDialog() == DialogResult.OK) //如果确定保存
            {
                if (sf.FileName == "") return; //如果没有输入文件名
                try
                {
                    StreamWriter sw = null; //文件流
                    sw = new StreamWriter(sf.FileName, false, Encoding.UTF8);
                    str = str + "R0," + RM692EX_R0.Text + "," + "G0," + RM692EX_G0.Text + "," + "B0," + RM692EX_B0.Text + "\r\n";
                    str = str + "R1," + RM692EX_R1.Text + "," + "G1," + RM692EX_G1.Text + "," + "B1," + RM692EX_B1.Text + "\r\n";
                    str = str + "R2," + RM692EX_R2.Text + "," + "G2," + RM692EX_G2.Text + "," + "B2," + RM692EX_B2.Text + "\r\n";
                    str = str + "R3," + RM692EX_R3.Text + "," + "G3," + RM692EX_G3.Text + "," + "B3," + RM692EX_B3.Text + "\r\n";
                    str = str + "R4," + RM692EX_R4.Text + "," + "G4," + RM692EX_G4.Text + "," + "B4," + RM692EX_B4.Text + "\r\n";
                    str = str + "R5," + RM692EX_R5.Text + "," + "G5," + RM692EX_G5.Text + "," + "B5," + RM692EX_B5.Text + "\r\n";
                    str = str + "R6," + RM692EX_R6.Text + "," + "G6," + RM692EX_G6.Text + "," + "B6," + RM692EX_B6.Text + "\r\n";
                    str = str + "R7," + RM692EX_R7.Text + "," + "G7," + RM692EX_G7.Text + "," + "B7," + RM692EX_B7.Text + "\r\n";
                    str = str + "R8," + RM692EX_R8.Text + "," + "G8," + RM692EX_G8.Text + "," + "B8," + RM692EX_B8.Text + "\r\n";
                    str = str + "R9," + RM692EX_R9.Text + "," + "G9," + RM692EX_G9.Text + "," + "B9," + RM692EX_B9.Text + "\r\n";
                    str = str + "R10," + RM692EX_R10.Text + "," + "G10," + RM692EX_G10.Text + "," + "B10," + RM692EX_B10.Text + "\r\n";
                    str = str + "R11," + RM692EX_R11.Text + "," + "G11," + RM692EX_G11.Text + "," + "B11," + RM692EX_B11.Text + "\r\n";
                    str = str + "R12," + RM692EX_R12.Text + "," + "G12," + RM692EX_G12.Text + "," + "B12," + RM692EX_B12.Text + "\r\n";
                    str = str + "R13," + RM692EX_R13.Text + "," + "G13," + RM692EX_G13.Text + "," + "B13," + RM692EX_B13.Text + "\r\n";
                    str = str + "R14," + RM692EX_R14.Text + "," + "G14," + RM692EX_G14.Text + "," + "B14," + RM692EX_B14.Text + "\r\n";
                    str = str + "R15," + RM692EX_R15.Text + "," + "G15," + RM692EX_G15.Text + "," + "B15," + RM692EX_B15.Text + "\r\n";
                    str = str + "R16," + RM692EX_R16.Text + "," + "G16," + RM692EX_G16.Text + "," + "B16," + RM692EX_B16.Text + "\r\n";
                    str = str + "R17," + RM692EX_R17.Text + "," + "G17," + RM692EX_G17.Text + "," + "B17," + RM692EX_B17.Text + "\r\n";
                    str = str + "R18," + RM692EX_R18.Text + "," + "G18," + RM692EX_G18.Text + "," + "B18," + RM692EX_B18.Text + "\r\n";
                    str = str + "R19," + RM692EX_R19.Text + "," + "G19," + RM692EX_G19.Text + "," + "B19," + RM692EX_B19.Text + "\r\n";
                    str = str + "R20," + RM692EX_R20.Text + "," + "G20," + RM692EX_G20.Text + "," + "B20," + RM692EX_B20.Text + "\r\n";
                    str = str + "R21," + RM692EX_R21.Text + "," + "G21," + RM692EX_G21.Text + "," + "B21," + RM692EX_B21.Text + "\r\n";
                    str = str + "R22," + RM692EX_R22.Text + "," + "G22," + RM692EX_G22.Text + "," + "B22," + RM692EX_B22.Text + "\r\n";
                    str = str + "R23," + RM692EX_R23.Text + "," + "G23," + RM692EX_G23.Text + "," + "B23," + RM692EX_B23.Text + "\r\n";
                    str = str + "R24," + RM692EX_R24.Text + "," + "G24," + RM692EX_G24.Text + "," + "B24," + RM692EX_B24.Text + "\r\n";
                    str = str + "R25," + RM692EX_R25.Text + "," + "G25," + RM692EX_G25.Text + "," + "B25," + RM692EX_B25.Text + "\r\n";
                    str = str + "R26," + RM692EX_R26.Text + "," + "G26," + RM692EX_G26.Text + "," + "B26," + RM692EX_B26.Text + "\r\n";

                    sw.WriteLine(str);
                    sw.Dispose(); //释放资源
                    sw.Close(); //关闭数据流
                    MessageBox.Show("Gamma保存OK！", "系统提示：");
                }
                catch
                {
                    MessageBox.Show("Gamma保存时发生错误，请再试！", "系统提示：");
                }
            }
        } //
        //Gamma载入
        private void RM692EX_LoadGamma_Click(object sender, EventArgs e)
        {
            OpenFileDialog of = new OpenFileDialog();
            string strtmp = "";
            of.Filter = @"csv文件(*.csv)|*.csv|所有文件(*.*)|*.*"; ;
            of.Title = "打开Gamma文件";

            if (of.ShowDialog() == DialogResult.OK)
            {
                //读取文本信息
                FileStream fs = new FileStream(of.FileName, FileMode.Open, FileAccess.Read);
                StreamReader streamReader = new StreamReader(fs, Encoding.GetEncoding("GBK"));
                streamReader.BaseStream.Seek(0, SeekOrigin.Begin);
                string strLine = streamReader.ReadLine();
                //循环读取文本信息
                while (!string.IsNullOrEmpty(strLine))
                {
                    strtmp = strtmp + strLine + ",";
                    strLine = streamReader.ReadLine(); //循环读取下一行
                }
                fs.Close(); //关闭流
                streamReader.Close();
                string[] arrtmp = strtmp.Split(new Char[] { ',' });
                RM692EX_R0.Text = arrtmp[1].ToString(); RM692EX_G0.Text = arrtmp[3].ToString(); RM692EX_B0.Text = arrtmp[5].ToString();
                RM692EX_R1.Text = arrtmp[7].ToString(); RM692EX_G1.Text = arrtmp[9].ToString(); RM692EX_B1.Text = arrtmp[11].ToString();
                RM692EX_R2.Text = arrtmp[13].ToString(); RM692EX_G2.Text = arrtmp[15].ToString(); RM692EX_B2.Text = arrtmp[17].ToString();
                RM692EX_R3.Text = arrtmp[19].ToString(); RM692EX_G3.Text = arrtmp[21].ToString(); RM692EX_B3.Text = arrtmp[23].ToString();
                RM692EX_R4.Text = arrtmp[25].ToString(); RM692EX_G4.Text = arrtmp[27].ToString(); RM692EX_B4.Text = arrtmp[29].ToString();
                RM692EX_R5.Text = arrtmp[31].ToString(); RM692EX_G5.Text = arrtmp[33].ToString(); RM692EX_B5.Text = arrtmp[35].ToString();
                RM692EX_R6.Text = arrtmp[37].ToString(); RM692EX_G6.Text = arrtmp[39].ToString(); RM692EX_B6.Text = arrtmp[41].ToString();
                RM692EX_R7.Text = arrtmp[43].ToString(); RM692EX_G7.Text = arrtmp[45].ToString(); RM692EX_B7.Text = arrtmp[47].ToString();
                RM692EX_R8.Text = arrtmp[49].ToString(); RM692EX_G8.Text = arrtmp[51].ToString(); RM692EX_B8.Text = arrtmp[53].ToString();
                RM692EX_R9.Text = arrtmp[55].ToString(); RM692EX_G9.Text = arrtmp[57].ToString(); RM692EX_B9.Text = arrtmp[59].ToString();
                RM692EX_R10.Text = arrtmp[61].ToString(); RM692EX_G10.Text = arrtmp[63].ToString(); RM692EX_B10.Text = arrtmp[65].ToString();
                RM692EX_R11.Text = arrtmp[67].ToString(); RM692EX_G11.Text = arrtmp[69].ToString(); RM692EX_B11.Text = arrtmp[71].ToString();
                RM692EX_R12.Text = arrtmp[73].ToString(); RM692EX_G12.Text = arrtmp[75].ToString(); RM692EX_B12.Text = arrtmp[77].ToString();
                RM692EX_R13.Text = arrtmp[79].ToString(); RM692EX_G13.Text = arrtmp[81].ToString(); RM692EX_B13.Text = arrtmp[83].ToString();
                RM692EX_R14.Text = arrtmp[85].ToString(); RM692EX_G14.Text = arrtmp[87].ToString(); RM692EX_B14.Text = arrtmp[89].ToString();
                RM692EX_R15.Text = arrtmp[91].ToString(); RM692EX_G15.Text = arrtmp[93].ToString(); RM692EX_B15.Text = arrtmp[95].ToString();
                RM692EX_R16.Text = arrtmp[97].ToString(); RM692EX_G16.Text = arrtmp[99].ToString(); RM692EX_B16.Text = arrtmp[101].ToString();
                RM692EX_R17.Text = arrtmp[103].ToString(); RM692EX_G17.Text = arrtmp[105].ToString(); RM692EX_B17.Text = arrtmp[107].ToString();
                RM692EX_R18.Text = arrtmp[109].ToString(); RM692EX_G18.Text = arrtmp[111].ToString(); RM692EX_B18.Text = arrtmp[113].ToString();
                RM692EX_R19.Text = arrtmp[115].ToString(); RM692EX_G19.Text = arrtmp[117].ToString(); RM692EX_B19.Text = arrtmp[119].ToString();
                RM692EX_R20.Text = arrtmp[121].ToString(); RM692EX_G20.Text = arrtmp[123].ToString(); RM692EX_B20.Text = arrtmp[125].ToString();
                RM692EX_R21.Text = arrtmp[127].ToString(); RM692EX_G21.Text = arrtmp[129].ToString(); RM692EX_B21.Text = arrtmp[131].ToString();
                RM692EX_R22.Text = arrtmp[133].ToString(); RM692EX_G22.Text = arrtmp[135].ToString(); RM692EX_B22.Text = arrtmp[137].ToString();
                RM692EX_R23.Text = arrtmp[139].ToString(); RM692EX_G23.Text = arrtmp[141].ToString(); RM692EX_B23.Text = arrtmp[143].ToString();
                RM692EX_R24.Text = arrtmp[145].ToString(); RM692EX_G24.Text = arrtmp[147].ToString(); RM692EX_B24.Text = arrtmp[149].ToString();
                RM692EX_R25.Text = arrtmp[151].ToString(); RM692EX_G25.Text = arrtmp[153].ToString(); RM692EX_B25.Text = arrtmp[155].ToString();
                RM692EX_R26.Text = arrtmp[157].ToString(); RM692EX_G26.Text = arrtmp[159].ToString(); RM692EX_B26.Text = arrtmp[161].ToString();
            }
        }
        #endregion



        private static byte Gamma_Auto_flag_dll = 0;
        private int Multi_gamma_txt_now_index = 0;
        private int Multi_gamma_txt_next_index;
        private int Multi_gamma_txt_line_count = 0;
        private int Multi_gamma_group_index = 0;
        byte tep = 0;
        private void Btn_flexible_gamma_Click(object sender, EventArgs e)
        {
            frmHelp.btnCal_Click(null, null);
            textBox1.Text = textBox1.Text + "[" + DateTime.Now.ToLongTimeString().ToString() + "]：" + "Casdk sensor connect! \r\n";
            ADB_Inter_root();
            textBox1.Text = textBox1.Text + "[" + DateTime.Now.ToLongTimeString().ToString() + "]：" + "ADB Shell/root connect! \r\n";
            Bist_pattern(255, 255, 255);
            Thread.Sleep(500);
            btn_RM692Ex_Calculate_Click(null, null);
            Multi_gamma_txt_now_index = 0;
            Multi_gamma_txt_line_count = this.RM692EX_Gamma_SetBox.Lines.GetUpperBound(0);
            Multi_flexible_gammas(Multi_gamma_txt_now_index, out Multi_gamma_txt_next_index);
        }
        private void btnAutoGamma_RM692EX_Click(object sender, EventArgs e)//255阶为例
        {
            double L_X, L_Y, L_Z;
            index = 0;
            flag1 = 1;
            flag2 = 1;
            flag3 = 1;

            //------------flexible gamma use------------------
            frmHelp.Gamma_auto_adjust_count = 0;
            Gamma_adjust_timeout_flag = false;
            //------------------------------------------------

            frmHelp.numPatternR.Value = Diven_Offset[index].NO;
            frmHelp.numPatternG.Value = Diven_Offset[index].NO;
            frmHelp.numPatternB.Value = Diven_Offset[index].NO;
            ///frmHelp.OLED_Show_Click(frmHelp.btnPatternW, e);
            ///log_cf ADB切图
            // ADB_bmp_Traffic(Diven_Offset[index].NO.ToString());
            //Bist_pattern((byte)(Diven_Offset[index].NO), (byte)(Diven_Offset[index].NO), (byte)(Diven_Offset[index].NO));

            btn_Bist_R.Value = Diven_Offset[index].NO;
            btn_Bist_G.Value = Diven_Offset[index].NO;
            btn_Bist_B.Value = Diven_Offset[index].NO;

            textBox1.Text = textBox1.Text + "[" + DateTime.Now.ToLongTimeString().ToString() + "]：" + "Gray : " + Diven_Offset[index].NO.ToString() + " Pass" + "\r\n";
            //Application.DoEvents();
            button_bist_Click(null, null);
            Thread.Sleep(200);

            T20.Enabled = true;
        }

        private void ADB_Send_String_Buf(string dataline)
        {
            string[] adbarr = dataline.Split(','); //以逗号分割，添加到数组
            byte[] ADB_buf = new byte[adbarr.Length];
            for (int i = 0; i < adbarr.Length; i++)
            {
                ADB_buf[i] = Convert.ToByte(adbarr[i], 16);
            }
            ADB_info_Traffic(ADB_buf, ADB_buf.Length);
        }

        private void Multi_flexible_gammas(int txt_index, out int list_view_index)
        {
            string strLine = Convert.ToString(RM692EX_Gamma_SetBox.Text);
            string[] arr = strLine.Split(','); //以逗号分割，添加到数组
            string perline = string.Empty;
            string perline0 = string.Empty;
            list_view_index = 0;

            if (RM692EX_Gamma_SetBox.Text == string.Empty)
            {
                MessageBox.Show("内容为空!");
            }
            else  //标记关键事件
            {
                try
                {
                    arr = strLine.Split('\n'); //以逗号分割，添加到数
                    for (int i = txt_index; i < arr.Length;)  //获取的行数
                    {
                        perline = arr[i];
                        perline = perline.Replace(" ", "");
                        perline = perline.Replace(";", "");
                        perline = perline.Replace("(", "");
                        perline = perline.Replace(")", "");
                        perline = perline.Replace("\r", "");
                        perline = perline.ToUpper();

                        if (perline.Contains("REGW"))
                        {
                            perline = perline.Replace("REGW", "");

                            string[] adbarr = perline.Split(','); //以逗号分割，添加到数组
                            byte[] ADB_buf = new byte[adbarr.Length];
                            for (int count = 0; count < adbarr.Length; count++)
                            {
                                ADB_buf[count] = Convert.ToByte(adbarr[count], 16);
                            }
                            ADB_info_Traffic(ADB_buf, ADB_buf.Length);
                            Thread.Sleep(5);

                            i = i + 1;
                        }
                        else if (perline.Contains("AOD_IN"))
                        {
                            //   cBx_AOD_Auto_Gamma.Checked = true;
                            //  i = i + 1;
                        }
                        else if (perline.Contains("AOD_OUT"))
                        {
                            //  cBx_AOD_Auto_Gamma.Checked = false;
                            // i = i + 1;
                        }
                        else if (perline.Contains("SAVE"))
                        {
                            string pathsave_file = System.Environment.CurrentDirectory + "\\l_gamma";
                            if (Directory.Exists(pathsave_file) == false)
                            {
                                Directory.CreateDirectory(pathsave_file);
                            }
                            string pathsave = System.Environment.CurrentDirectory + "\\l_gamma" + "\\gamma" + Multi_gamma_group_index.ToString() + ".csv";
                            Multi_SaveGamma(pathsave);

                            RM692EX_Gamma_Map(byte.Parse(Gamma_Band_Select.Text));
                            Thread.Sleep(500);
                            i = i + 1;
                        }
                        else if (perline.Contains("GAMMA_SET"))
                        {
                            frmHelp.cbxlogic_go.Checked = false;
                            perline = perline.Replace("GAMMA_SET", "");
                            perline = perline.Replace("_", "");
                            Multi_gamma_group_index = Convert.ToInt32(perline);
                            //VT6110_Gamma_Group.Value = Multi_gamma_group_index - 1;
                            Gamma_Band_Select.Text = Multi_gamma_group_index.ToString();
                            Application.DoEvents();

                            byte[] gamma_reg = new byte[2] { 0xfe, 0x22 };
                            ADB_info_Traffic(gamma_reg, 2);
                            gamma_reg = new byte[2] { 0x5f, 0x02 };//Disable Gamma int
                            ADB_info_Traffic(gamma_reg, 2);

                            gamma_reg = new byte[2] { 0xfe, 0x70 };
                            ADB_info_Traffic(gamma_reg, 2);
                            gamma_reg = new byte[2] { 0x28, 0x00 };//Disable Gamma int
                            ADB_info_Traffic(gamma_reg, 2);

                            gamma_reg = new byte[2] { 0xfe, 0x40 };
                            ADB_info_Traffic(gamma_reg, 2);

                            gamma_reg = new byte[2] { 0xDA, 0xC8 };
                            ADB_info_Traffic(gamma_reg, 2);

                            gamma_reg = new byte[2] { 0xD7, byte.Parse(Gamma_Band_Select.Text) };
                            ADB_info_Traffic(gamma_reg, 2);
                            //----------------------------------------------
                            gamma_reg = new byte[2] { 0xFE, 0x78 };
                            ADB_info_Traffic(gamma_reg, 2);

                            gamma_reg = new byte[2] { 0xF4, 0xC0 };
                            ADB_info_Traffic(gamma_reg, 2);

                            gamma_reg = new byte[2] { 0xF5, byte.Parse(Gamma_Band_Select.Text) };
                            ADB_info_Traffic(gamma_reg, 2);

                            gamma_reg = new byte[2] { 0xfe, 0x50 };
                            ADB_info_Traffic(gamma_reg, 2);

                            ArrayList list = new ArrayList();
                            do
                            {
                                i = i + 1;
                                perline = arr[i];
                                list.Add(perline + "\n");
                            } while (perline.Contains("GAMMA_TUNING") == false);
                            string[] gamma_set_lines = (string[])list.ToArray(typeof(string));
                            Text_Processing(gamma_set_lines);
                            try
                            {
                                string pathsave_file = System.Environment.CurrentDirectory + "\\l_gamma";
                                if (Directory.Exists(pathsave_file) == false)
                                {
                                    Directory.CreateDirectory(pathsave_file);
                                }
                                string pathload = System.Environment.CurrentDirectory + "\\l_gamma" + "\\gamma" + Multi_gamma_group_index.ToString() + ".csv";
                                Multi_LoadGamma(pathload);
                                Application.DoEvents();
                                Thread.Sleep(200);


                                //byte[] Gamma_R_MSB = new byte[2] { gamma_r[(17 - 0) * 2 - 2 - index], (byte)(((int)(RM692EX_R16.Value) >> 8) & 0x0f) };
                                //byte[] Gamma_R_LSB = new byte[2] { gamma_r[(17 - 0) * 2 - 1 - index], (byte)((int)(RM692EX_R16.Value) & 0xff) };

                                //byte[] Gamma_G_MSB = new byte[2] { gamma_g[(17 - 0) * 2 - 2 - index], (byte)(((int)(RM692EX_G16.Value) >> 8) & 0x0f) };
                                //byte[] Gamma_G_LSB = new byte[2] { gamma_g[(17 - 0) * 2 - 1 - index], (byte)((int)(RM692EX_G16.Value) & 0xff) };

                                //byte[] Gamma_B_MSB = new byte[2] { gamma_b[(17 - 0) * 2 - 2 - index], (byte)(((int)(RM692EX_B16.Value) >> 8) & 0x0f) };
                                //byte[] Gamma_B_LSB = new byte[2] { gamma_b[(17 - 0) * 2 - 1 - index], (byte)((int)(RM692EX_B16.Value) & 0xff) };


                                //byte[] Gamma_Page = new byte[2] { 0xfe, 0x50 };

                                //ADB_info_Traffic(Gamma_Page, 2);

                                //ADB_info_Traffic(Gamma_R_MSB, 2);
                                //ADB_info_Traffic(Gamma_R_LSB, 2);

                                //ADB_info_Traffic(Gamma_B_MSB, 2);
                                //ADB_info_Traffic(Gamma_B_LSB, 2);

                                //ADB_info_Traffic(Gamma_G_MSB, 2);
                                //ADB_info_Traffic(Gamma_B_LSB, 2);
                            }
                            catch {; }

                            ADB_Inter_root();
                            Bist_pattern(255, 255, 255);
                            Thread.Sleep(500);
                            btnAutoGamma_RM692EX_Click(null, null); //替换成相应IC的自动gamma启动函数名
                            //log_cf 节约时间
                            list_view_index = i;
                            break;
                        }

                        else if (perline.Contains("EXITGAMMA"))
                        {
                            RM692EX_Exit_Gamma_Click(null, null);
                            i = i + 1;
                        }
                        else if (perline.Contains("DELAY"))
                        {
                            Thread.Sleep(200);
                            i = i + 1;
                        }
                        else if (perline.Contains("CALU"))
                        {
                            RM692EX_Calu_90HZ_Gamma();
                            // RM692EX_90HZ_Gamma_Write();
                            textBox1.Text = textBox1.Text + "Gamma caluate finish!\r\n";
                            i = i + 1;
                        }
                        else if (perline.Contains("OTP"))
                        {
                            RM692EX_Bist_OFF(null, null);
                            Thread.Sleep(100);
                            RM692EX_120HZ_Gamma_Write(null, null);
                            Thread.Sleep(100);
                            RM692EX_90HZ_Gamma_Write(null, null);
                            Thread.Sleep(100);
                            RM692EX_60HZ_Gamma_Write(null, null);
                            Thread.Sleep(100);
                            RM692EX_Exit_Gamma_Click(null, null);
                            Thread.Sleep(100);
                            RM692EX_Gamma_OTP(null, null);
                            i = i + 1;
                        }
                        else
                        {
                            i = i + 1;
                        }
                        progressBar1.Value = Convert.ToInt32((double)i / arr.Length * 100);
                        label14.Text = progressBar1.Value.ToString() + "%";
                        Application.DoEvents();
                    }
                }
                catch
                {
                    MessageBox.Show("输入格式有误！");
                }
            }
        }
        private void Multi_SaveGamma(string path)
        {
            try
            {
                string str = "保存Gamma文件";
                StreamWriter sw = null; //文件流
                sw = new StreamWriter(path, false, Encoding.UTF8);
                str = str + "R0," + RM692EX_R0.Text + "," + "G0," + RM692EX_G0.Text + "," + "B0," + RM692EX_B0.Text + "\r\n";
                str = str + "R1," + RM692EX_R1.Text + "," + "G1," + RM692EX_G1.Text + "," + "B1," + RM692EX_B1.Text + "\r\n";
                str = str + "R2," + RM692EX_R2.Text + "," + "G2," + RM692EX_G2.Text + "," + "B2," + RM692EX_B2.Text + "\r\n";
                str = str + "R3," + RM692EX_R3.Text + "," + "G3," + RM692EX_G3.Text + "," + "B3," + RM692EX_B3.Text + "\r\n";
                str = str + "R4," + RM692EX_R4.Text + "," + "G4," + RM692EX_G4.Text + "," + "B4," + RM692EX_B4.Text + "\r\n";
                str = str + "R5," + RM692EX_R5.Text + "," + "G5," + RM692EX_G5.Text + "," + "B5," + RM692EX_B5.Text + "\r\n";
                str = str + "R6," + RM692EX_R6.Text + "," + "G6," + RM692EX_G6.Text + "," + "B6," + RM692EX_B6.Text + "\r\n";
                str = str + "R7," + RM692EX_R7.Text + "," + "G7," + RM692EX_G7.Text + "," + "B7," + RM692EX_B7.Text + "\r\n";
                str = str + "R8," + RM692EX_R8.Text + "," + "G8," + RM692EX_G8.Text + "," + "B8," + RM692EX_B8.Text + "\r\n";
                str = str + "R9," + RM692EX_R9.Text + "," + "G9," + RM692EX_G9.Text + "," + "B9," + RM692EX_B9.Text + "\r\n";
                str = str + "R10," + RM692EX_R10.Text + "," + "G10," + RM692EX_G10.Text + "," + "B10," + RM692EX_B10.Text + "\r\n";
                str = str + "R11," + RM692EX_R11.Text + "," + "G11," + RM692EX_G11.Text + "," + "B11," + RM692EX_B11.Text + "\r\n";
                str = str + "R12," + RM692EX_R12.Text + "," + "G12," + RM692EX_G12.Text + "," + "B12," + RM692EX_B12.Text + "\r\n";
                str = str + "R13," + RM692EX_R13.Text + "," + "G13," + RM692EX_G13.Text + "," + "B13," + RM692EX_B13.Text + "\r\n";
                str = str + "R14," + RM692EX_R14.Text + "," + "G14," + RM692EX_G14.Text + "," + "B14," + RM692EX_B14.Text + "\r\n";
                str = str + "R15," + RM692EX_R15.Text + "," + "G15," + RM692EX_G15.Text + "," + "B15," + RM692EX_B15.Text + "\r\n";
                str = str + "R16," + RM692EX_R16.Text + "," + "G16," + RM692EX_G16.Text + "," + "B16," + RM692EX_B16.Text + "\r\n";
                str = str + "R17," + RM692EX_R17.Text + "," + "G17," + RM692EX_G17.Text + "," + "B17," + RM692EX_B17.Text + "\r\n";
                str = str + "R18," + RM692EX_R18.Text + "," + "G18," + RM692EX_G18.Text + "," + "B18," + RM692EX_B18.Text + "\r\n";
                str = str + "R19," + RM692EX_R19.Text + "," + "G19," + RM692EX_G19.Text + "," + "B19," + RM692EX_B19.Text + "\r\n";
                str = str + "R20," + RM692EX_R20.Text + "," + "G20," + RM692EX_G20.Text + "," + "B20," + RM692EX_B20.Text + "\r\n";
                str = str + "R21," + RM692EX_R21.Text + "," + "G21," + RM692EX_G21.Text + "," + "B21," + RM692EX_B21.Text + "\r\n";
                str = str + "R22," + RM692EX_R22.Text + "," + "G22," + RM692EX_G22.Text + "," + "B22," + RM692EX_B22.Text + "\r\n";
                str = str + "R23," + RM692EX_R23.Text + "," + "G23," + RM692EX_G23.Text + "," + "B23," + RM692EX_B23.Text + "\r\n";
                str = str + "R24," + RM692EX_R24.Text + "," + "G24," + RM692EX_G24.Text + "," + "B24," + RM692EX_B24.Text + "\r\n";
                str = str + "R25," + RM692EX_R25.Text + "," + "G25," + RM692EX_G25.Text + "," + "B25," + RM692EX_B25.Text + "\r\n";
                str = str + "R26," + RM692EX_R26.Text + "," + "G26," + RM692EX_G26.Text + "," + "B26," + RM692EX_B26.Text + "\r\n";
                sw.WriteLine(str);
                sw.Dispose(); //释放资源
                sw.Close(); //关闭数据流
            }
            catch
            {
                MessageBox.Show("Gamma保存时发生错误，请再试！", "系统提示：");
            }
        } //
        private void Text_Processing(string[] txt_path)
        {
            double L_X, L_Y, L_Z;
            string[] arr = txt_path;
            {
                try
                {
                    Diven_Gamma.Clear();
                    Diven_Offset.Clear();
                    for (int i = 0; i < arr.Length; i++)
                    {
                        string line = arr[i];
                        line = line.Replace(" ", "");
                        if (line.Contains("TUNING_SPEC"))
                        {
                            line = line.Replace("TUNING_SPEC", "");
                            line = line.Replace("(", "");
                            line = line.Replace(")", "");
                            string[] arr0 = line.Split(',');
                            Setting.Gamma_Property Gamma = new Setting.Gamma_Property(Convert.ToInt32(arr0[0]), Convert.ToDouble(arr0[1]), Convert.ToDouble(arr0[2]), Convert.ToDouble(arr0[3]), Convert.ToDouble(arr0[4]));
                            Diven_Gamma.Add(Gamma);
                        }
                        else if (line.Contains("LXY_SPEC"))
                        {
                            line = line.Replace("LXY_SPEC", "");
                            line = line.Replace("(", "");
                            line = line.Replace(")", "");
                            string[] arr1 = line.Split(',');
                            //将Offset对象放入集合
                            Setting.Offset_Property Offset = new Setting.Offset_Property(Convert.ToInt32(arr1[0]), Convert.ToDouble(arr1[1]), Convert.ToDouble(arr1[2]), Convert.ToDouble(arr1[3]));
                            Diven_Offset.Add(Offset);
                        }
                    }
                }
                catch
                {
                    MessageBox.Show("格式输入有误");
                }
            }
            try  //获取调整spec
            {
                txt2EX_lumoff.Text = Diven_Offset[0].LUM_OFF.ToString();
                txt2EX_Xoff.Text = Diven_Offset[0].X_OFF.ToString();
                txt2EX_Yoff.Text = Diven_Offset[0].Y_OFF.ToString();

                Total_points = Diven_Gamma[0].Points_num;
                D_lums = new double[Total_points, 3];
                D_lum = new double[Total_points];

                txt2EX_Gamma.Text = Diven_Gamma[0].Curve.ToString();
                txt2EX_lum.Text = Diven_Gamma[0].Lum.ToString();
                txt2EX_X.Text = Diven_Gamma[0].X.ToString();
                txt2EX_Y.Text = Diven_Gamma[0].Y.ToString();

                for (int i = 0; i < Total_points; i++)
                {   //Diven_Offset[p].NO 灰阶绑点                 
                    D_lum[i] = (Math.Pow((Diven_Offset[i].NO / 255.0), Diven_Gamma[0].Curve) * Diven_Gamma[0].Lum); //每绑点目标亮度
                    CaSDKObj.Lvxy_to_XYZ(D_lum[i], Diven_Gamma[0].X, Diven_Gamma[0].Y, out L_X, out L_Y, out L_Z);//X/Z/Z转换
                    D_lums[i, 0] = L_X; //R/B/G分亮度计算
                    D_lums[i, 1] = L_Y;
                    D_lums[i, 2] = L_Z;
                }
            }
            catch
            {
                MessageBox.Show("格式输入有误");
            }
        }
        private void RM692EX_AUTO_gamma(Object myObject, EventArgs myEventArgs)
        {
            double L_X, L_Y, L_Z, lumr, lumg, lumb;
            frmHelp.Msr_CASDK();

            if (Gamma_adjust_timeout_flag == true)//如果是0绑点
            {
                flag1 = 0;
                flag2 = 0;
                flag3 = 0;
            }
            else
            {
                try
                {
                    if ((frmHelp.CASDK_LUM < (D_lum[index] + D_lum[index] * Diven_Offset[index].LUM_OFF / 100)) && (frmHelp.CASDK_LUM > (D_lum[index] - D_lum[index] * Diven_Offset[index].LUM_OFF / 100)))//G
                    { flag1 = 0; }
                    else
                    { flag1 = 1; }
                    if ((frmHelp.CASDK_x < (Diven_Gamma[0].X + Diven_Offset[index].X_OFF)) && (frmHelp.CASDK_x > (Diven_Gamma[0].X - Diven_Offset[index].X_OFF)) && flag1 == 0)//R
                    { flag3 = 0; }
                    else
                    { flag3 = 1; }
                    if ((frmHelp.CASDK_y < (Diven_Gamma[0].Y + Diven_Offset[index].Y_OFF)) && (frmHelp.CASDK_y > (Diven_Gamma[0].Y - Diven_Offset[index].Y_OFF)) && flag1 == 0 && flag3 == 0)//B
                    { flag2 = 0; }
                    else
                    { flag2 = 1; }
                }
                catch
                {; }
            }

            if (flag1 == 0 && flag2 == 0 && flag3 == 0)
            {
                T20.Enabled = false;
                //reset-----
                Gamma_adjust_timeout_flag = false;
                frmHelp.Gamma_auto_adjust_count = 0;
                //--------
                index++;

                if (index == Diven_Gamma[0].Points_num)
                {
                    textBox1.Text = textBox1.Text + "Gamma band " + Gamma_Band_Select.Text + " tuning finish!\r\n";
                    //---------------------------------add-------------------------------------
                    if (Multi_gamma_txt_next_index < Multi_gamma_txt_line_count)
                    {
                        ADB_Inter_root();//重新root
                        Multi_gamma_txt_now_index = Multi_gamma_txt_next_index;
                        Multi_flexible_gammas(Multi_gamma_txt_now_index, out Multi_gamma_txt_next_index);
                    }
                    //---------------------------------add-------------------------------------
                }

                if (index < Diven_Gamma[0].Points_num) //绑点切换部分
                {
                    frmHelp.numPatternR.Value = Diven_Offset[index].NO;//固定255灰阶
                    frmHelp.numPatternG.Value = Diven_Offset[index].NO;
                    frmHelp.numPatternB.Value = Diven_Offset[index].NO;
                    //log_cf  后续添加ADB指令切图
                    // Bist_pattern((byte)(Diven_Offset[index].NO), (byte)(Diven_Offset[index].NO), (byte)(Diven_Offset[index].NO));

                    btn_Bist_R.Value = Diven_Offset[index].NO;
                    btn_Bist_G.Value = Diven_Offset[index].NO;
                    btn_Bist_B.Value = Diven_Offset[index].NO;
                    Application.DoEvents();

                    textBox1.Text = textBox1.Text + "[" + DateTime.Now.ToLongTimeString().ToString() + "]：" + "Gray : " + Diven_Offset[index].NO.ToString() + " Pass" + "\r\n";

                    button_bist_Click(null, null);
                    Thread.Sleep(200);
                    frmHelp.Msr_CASDK();
                    flag1 = 1;
                    flag2 = 1;
                    flag3 = 1;

                    T20.Enabled = true;
                }

            }


            else  //调整部分
            {
                decimal r = 0, g = 0, b = 0;
                CaSDKObj.Lvxy_to_XYZ(frmHelp.CASDK_LUM, frmHelp.CASDK_x, frmHelp.CASDK_y, out L_X, out L_Y, out L_Z);
                lumr = L_X;
                lumg = L_Y;
                lumb = L_Z;
                //textBox1.Text = textBox1.Text + "[" + DateTime.Now.ToLongTimeString().ToString() + "]：" + "X: " + frmHelp.CASDK_x.ToString() + "\r\n";
                //textBox1.Text = textBox1.Text + "[" + DateTime.Now.ToLongTimeString().ToString() + "]：" + "Y: " + frmHelp.CASDK_y.ToString() + "\r\n";
                //textBox1.Text = textBox1.Text + "[" + DateTime.Now.ToLongTimeString().ToString() + "]：" + "LV: " + frmHelp.CASDK_LUM.ToString() + "\r\n";
                try
                {
                    switch (index)
                    {
                        case 0: //9
                            {
                                frmHelp.Gamma_auto_adjust_count_set = 80;
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], RM692EX_R16.Value, RM692EX_G16.Value, RM692EX_B16.Value, Diven_Offset[index].NO, 0xfff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                RM692EX_R16.Value = r;
                                RM692EX_G16.Value = g;
                                RM692EX_B16.Value = b;

                                if (RM692EX_R16.Value <= RM692EX_R15.Value)
                                {
                                    RM692EX_R15.Value = r - 5;
                                }
                                else if (RM692EX_R16.Value - RM692EX_R15.Value > 0xf0)
                                {
                                    RM692EX_R15.Value = RM692EX_R16.Value - 0xf0;
                                }
                                if (RM692EX_G16.Value <= RM692EX_G15.Value)
                                {
                                    RM692EX_G15.Value = g - 5;
                                }
                                else if (RM692EX_G16.Value - RM692EX_G15.Value > 0xf0)
                                {
                                    RM692EX_G15.Value = RM692EX_G16.Value - 0xf0;
                                }
                                if (RM692EX_B16.Value <= RM692EX_B15.Value)
                                {
                                    RM692EX_B15.Value = b - 5;
                                }
                                else if (RM692EX_B16.Value - RM692EX_B15.Value > 0xf0)
                                {
                                    RM692EX_B15.Value = RM692EX_B16.Value - 0xf0;
                                }

                            }
                            break;
                        case 1: //9
                            {
                                frmHelp.Gamma_auto_adjust_count_set = lumg < 0.1 ? 45 : 50;
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], RM692EX_R15.Value, RM692EX_G15.Value, RM692EX_B15.Value, Diven_Offset[index].NO, 0xfff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                if (RM692EX_R16.Value - r > 0xf0)
                                {
                                    RM692EX_R15.Value = RM692EX_R16.Value - 0xf0;
                                }
                                else
                                {
                                    RM692EX_R15.Value = r;
                                }
                                if (RM692EX_G16.Value - g > 0xfe)
                                {
                                    RM692EX_G15.Value = RM692EX_G16.Value - 0xfe;
                                }
                                else
                                {
                                    RM692EX_G15.Value = g;
                                }
                                if (RM692EX_B16.Value - b > 0xfe)
                                {
                                    RM692EX_B15.Value = RM692EX_B16.Value - 0xfe;
                                }
                                else
                                {
                                    RM692EX_B15.Value = b;
                                }
                            }
                            break;
                        case 2: //9
                            {
                                frmHelp.Gamma_auto_adjust_count_set = lumg < 0.1 ? 40 : 50;
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], RM692EX_R14.Value, RM692EX_G14.Value, RM692EX_B14.Value, Diven_Offset[index].NO, 0xfff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                RM692EX_R14.Value = r;
                                RM692EX_G14.Value = g;
                                RM692EX_B14.Value = b;

                            }
                            break;
                        case 3: //9
                            {
                                frmHelp.Gamma_auto_adjust_count_set = lumg < 0.1 ? 40 : 50;
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], RM692EX_R13.Value, RM692EX_G13.Value, RM692EX_B13.Value, Diven_Offset[index].NO, 0xfff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                RM692EX_R13.Value = r;
                                RM692EX_G13.Value = g;
                                RM692EX_B13.Value = b;

                            }
                            break;

                        case 4: //9
                            {
                                frmHelp.Gamma_auto_adjust_count_set = lumg < 0.1 ? 40 : 50;
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], RM692EX_R12.Value, RM692EX_G12.Value, RM692EX_B12.Value, Diven_Offset[index].NO, 0xfff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                RM692EX_R12.Value = r;
                                RM692EX_G12.Value = g;
                                RM692EX_B12.Value = b;

                            }
                            break;

                        case 5: //9
                            {
                                frmHelp.Gamma_auto_adjust_count_set = lumg < 0.1 ? 40 : 50;
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], RM692EX_R11.Value, RM692EX_G11.Value, RM692EX_B11.Value, Diven_Offset[index].NO, 0xfff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                RM692EX_R11.Value = r;
                                RM692EX_G11.Value = g;
                                RM692EX_B11.Value = b;

                            }
                            break;
                        case 6: //9
                            {
                                frmHelp.Gamma_auto_adjust_count_set = lumg < 0.1 ? 40 : 50;
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], RM692EX_R10.Value, RM692EX_G10.Value, RM692EX_B10.Value, Diven_Offset[index].NO, 0xfff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                RM692EX_R10.Value = r;
                                RM692EX_G10.Value = g;
                                RM692EX_B10.Value = b;

                            }
                            break;
                        case 7: //9
                            {
                                frmHelp.Gamma_auto_adjust_count_set = lumg < 0.1 ? 40 : 50;
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], RM692EX_R9.Value, RM692EX_G9.Value, RM692EX_B9.Value, Diven_Offset[index].NO, 0xfff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                RM692EX_R9.Value = r;
                                RM692EX_G9.Value = g;
                                RM692EX_B9.Value = b;

                            }
                            break;
                        case 8: //9
                            {
                                frmHelp.Gamma_auto_adjust_count_set = lumg < 0.1 ? 40 : 50;
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], RM692EX_R8.Value, RM692EX_G8.Value, RM692EX_B8.Value, Diven_Offset[index].NO, 0xfff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                RM692EX_R8.Value = r;
                                RM692EX_G8.Value = g;
                                RM692EX_B8.Value = b;

                            }
                            break;
                        case 9: //9
                            {
                                frmHelp.Gamma_auto_adjust_count_set = lumg < 0.1 ? 40 : 50;
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], RM692EX_R7.Value, RM692EX_G7.Value, RM692EX_B7.Value, Diven_Offset[index].NO, 0xfff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                RM692EX_R7.Value = r;
                                RM692EX_G7.Value = g;
                                RM692EX_B7.Value = b;

                            }
                            break;
                        case 10: //9
                            {
                                frmHelp.Gamma_auto_adjust_count_set = lumg < 0.1 ? 35 : 50;
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], RM692EX_R6.Value, RM692EX_G6.Value, RM692EX_B6.Value, Diven_Offset[index].NO, 0xfff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                RM692EX_R6.Value = r;
                                RM692EX_G6.Value = g;
                                RM692EX_B6.Value = b;

                            }
                            break;
                        case 11: //9
                            {
                                frmHelp.Gamma_auto_adjust_count_set = lumg < 0.1 ? 30 : 50;
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], RM692EX_R5.Value, RM692EX_G5.Value, RM692EX_B5.Value, Diven_Offset[index].NO, 0xfff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }

                                RM692EX_R5.Value = r;
                                RM692EX_G5.Value = g;
                                RM692EX_B5.Value = b;

                                //  RM692EX_auto_gamma_write(gamma_r[index], Convert.ToUInt16(RM692EX_R5.Value), gamma_g[index], Convert.ToUInt16(RM692EX_G5.Value), gamma_b[index], Convert.ToUInt16(RM692EX_B5.Value));
                            }
                            break;
                        case 12: //9
                            {
                                frmHelp.Gamma_auto_adjust_count_set = lumg < 0.1 ? 25 : 40;
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], RM692EX_R4.Value, RM692EX_G4.Value, RM692EX_B4.Value, Diven_Offset[index].NO, 0xfff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                RM692EX_R4.Value = r;
                                RM692EX_G4.Value = g;
                                RM692EX_B4.Value = b;

                                //  RM692EX_auto_gamma_write(gamma_r[index], Convert.ToUInt16(RM692EX_R4.Value), gamma_g[index], Convert.ToUInt16(RM692EX_G4.Value), gamma_b[index], Convert.ToUInt16(RM692EX_B4.Value));
                            }
                            break;
                        case 13: //9
                            {
                                frmHelp.Gamma_auto_adjust_count_set = lumg < 0.1 ? 20 : 40;
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], RM692EX_R3.Value, RM692EX_G3.Value, RM692EX_B3.Value, Diven_Offset[index].NO, 0xfff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                RM692EX_R3.Value = r;
                                RM692EX_G3.Value = g;
                                RM692EX_B3.Value = b;
                                // RM692EX_auto_gamma_write(gamma_r[index], Convert.ToUInt16(RM692EX_R3.Value), gamma_g[index], Convert.ToUInt16(RM692EX_G3.Value), gamma_b[index], Convert.ToUInt16(RM692EX_B3.Value));
                            }
                            break;
                        case 14: //9
                            {
                                frmHelp.Gamma_auto_adjust_count_set = lumg < 0.1 ? 15 : 30;
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], RM692EX_R2.Value, RM692EX_G2.Value, RM692EX_B2.Value, Diven_Offset[index].NO, 0xfff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                RM692EX_R2.Value = r;
                                RM692EX_G2.Value = g;
                                RM692EX_B2.Value = b;
                                //  RM692EX_auto_gamma_write(gamma_r[index], Convert.ToUInt16(RM692EX_R2.Value), gamma_g[index], Convert.ToUInt16(RM692EX_G2.Value), gamma_b[index], Convert.ToUInt16(RM692EX_B2.Value));
                            }
                            break;
                        case 15: //9
                            {
                                Gamma_adjust_timeout_flag = true;
                                //  frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], RM692EX_R1.Value, RM692EX_G1.Value, RM692EX_B1.Value, Diven_Offset[index].NO, 0xfff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                RM692EX_R1.Value = (int)RM692EX_R2.Value / 2;
                                RM692EX_G1.Value = (int)RM692EX_G2.Value / 2;
                                RM692EX_B1.Value = (int)RM692EX_B2.Value / 2;

                                if (RM692EX_R2.Value - RM692EX_R1.Value > 0xff)
                                {
                                    RM692EX_R1.Value = RM692EX_R2.Value - 0xfe;
                                }
                                if (RM692EX_G2.Value - RM692EX_G1.Value > 0xff)
                                {
                                    RM692EX_G1.Value = RM692EX_G2.Value - 0xfe;
                                }
                                if (RM692EX_B2.Value - RM692EX_B1.Value > 0xff)
                                {
                                    RM692EX_B1.Value = RM692EX_B2.Value - 0xfe;
                                }
                                //  RM692EX_auto_gamma_write(gamma_r[index], Convert.ToUInt16(RM692EX_R1.Value), gamma_g[index], Convert.ToUInt16(RM692EX_G1.Value), gamma_b[index], Convert.ToUInt16(RM692EX_B1.Value));
                            }
                            break;
                        case 16: //
                            {
                                Gamma_adjust_timeout_flag = true;
                                RM692EX_R0.Value = 0;
                                RM692EX_G0.Value = 0;
                                RM692EX_B0.Value = 0;
                                // T20.Enabled = false;
                            }
                            break;
                        default: break;
                    }


                    byte[] Gamma_R_MSB = new byte[2] { gamma_r[(17 - index) * 2 - 2], (byte)(((int)(r) >> 8) & 0x0f) };
                    byte[] Gamma_R_LSB = new byte[2] { gamma_r[(17 - index) * 2 - 1], (byte)((int)(r) & 0xff) };

                    byte[] Gamma_G_MSB = new byte[2] { gamma_g[(17 - index) * 2 - 2], (byte)(((int)(g) >> 8) & 0x0f) };
                    byte[] Gamma_G_LSB = new byte[2] { gamma_g[(17 - index) * 2 - 1], (byte)((int)(g) & 0xff) };

                    byte[] Gamma_B_MSB = new byte[2] { gamma_b[(17 - index) * 2 - 2], (byte)(((int)(b) >> 8) & 0x0f) };
                    byte[] Gamma_B_LSB = new byte[2] { gamma_b[(17 - index) * 2 - 1], (byte)((int)(b) & 0xff) };

                    byte[] Gamma_Page = new byte[2] { 0xfe, 0x50 };
                    ADB_info_Traffic(Gamma_Page, 2);
                    ADB_info_Traffic(Gamma_R_MSB, 2);
                    ADB_info_Traffic(Gamma_R_LSB, 2);
                    ADB_info_Traffic(Gamma_G_MSB, 2);
                    ADB_info_Traffic(Gamma_G_LSB, 2);
                    ADB_info_Traffic(Gamma_B_MSB, 2);
                    ADB_info_Traffic(Gamma_B_LSB, 2);


                    // T20.Enabled = true;
                    /*
                    string GammValueArr = "fe,50\r\n";
                    GammValueArr +=
                        Gamma_R_MSB[0].ToString("X") + "," + Gamma_R_MSB[1].ToString("X") + "\r\n" +
                        Gamma_R_LSB[0].ToString("X") + "," + Gamma_R_LSB[1].ToString("X") + "\r\n" +
                        Gamma_G_MSB[0].ToString("X") + "," + Gamma_G_MSB[1].ToString("X") + "\r\n" +
                        Gamma_G_LSB[0].ToString("X") + "," + Gamma_G_LSB[1].ToString("X") + "\r\n" +
                        Gamma_B_MSB[0].ToString("X") + "," + Gamma_B_MSB[1].ToString("X") + "\r\n" +
                        Gamma_B_LSB[0].ToString("X") + "," + Gamma_B_LSB[1].ToString("X");

                    ADB_info_Traffic(GammValueArr);*/

                    //Application.DoEvents();
                    //log_cf 添加adb写入指令

                    //
                }
                catch
                {
                    T20.Enabled = false;
                    MessageBox.Show("Tuning error! please try again!");
                }
            }
        }
        public void ADB_Inter_root()
        {
            System.Diagnostics.Process p1 = new System.Diagnostics.Process();
            p1.StartInfo.FileName = Application.StartupPath + @"\Resources\adb.exe";
            //是否使用操作系统shell启动
            p1.StartInfo.UseShellExecute = false;
            // 接受来自调用程序的输入信息
            //p1.StartInfo.RedirectStandardInput = true;
            //输出信息
            p1.StartInfo.RedirectStandardOutput = true;
            // 输出错误
            p1.StartInfo.RedirectStandardError = true;
            //不显示程序窗口
            p1.StartInfo.CreateNoWindow = true;
            //应该触发退出事件
            p1.EnableRaisingEvents = true;
            //程序退出事件
            p1.Exited += (s, er) =>
            {
                Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "完成");
            };
            //程序输出消息事件
            p1.OutputDataReceived += (o, er) =>
            {
                if (string.IsNullOrWhiteSpace(er?.Data))
                {
                    return;
                }
                Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "程序执行消息：" + er.Data);
            };
            string cmdstr = @"root";  //注意最后有一个空格
            p1.StartInfo.Arguments = cmdstr;
            p1.Start();
            //p1.BeginOutputReadLine();
            p1.WaitForExit(); //等待程序执行完退出进程
        }
        public void ADB_bmp_Traffic(string gray)
        {
            System.Diagnostics.Process p1 = new System.Diagnostics.Process();
            p1.StartInfo.FileName = Application.StartupPath + @"\Resources\adb.exe";
            //是否使用操作系统shell启动
            p1.StartInfo.UseShellExecute = false;
            // 接受来自调用程序的输入信息
            //p1.StartInfo.RedirectStandardInput = true;
            //输出信息
            p1.StartInfo.RedirectStandardOutput = true;
            // 输出错误
            p1.StartInfo.RedirectStandardError = true;
            //不显示程序窗口
            p1.StartInfo.CreateNoWindow = true;
            //应该触发退出事件
            p1.EnableRaisingEvents = true;

            string cmdstr = @"shell  am force-stop com.tools.cit";  //注意最后有一个空格
            p1.StartInfo.Arguments = cmdstr;
            p1.Start();
            p1.WaitForExit(); //等待程序执行完退出进程

            cmdstr = @"shell  am start -n com.tools.cit/.lcd.LCDGrayTest --ei lcdCount " + gray;  //注意最后有一个空格
            p1.StartInfo.Arguments = cmdstr;
            p1.Start();
            //p1.WaitForExit(); //等待程序执行完退出进程

            // Application.DoEvents();
            p1.Close();
        }

        public void ADB_info_Traffic(byte[] data, int length)
        {
            System.Diagnostics.Process p1 = new System.Diagnostics.Process();
            p1.StartInfo.FileName = Application.StartupPath + @"\Resources\adb.exe";
            //是否使用操作系统shell启动
            p1.StartInfo.UseShellExecute = false;
            // 接受来自调用程序的输入信息
            //p1.StartInfo.RedirectStandardInput = true;
            //输出信息
            p1.StartInfo.RedirectStandardOutput = true;
            // 输出错误
            p1.StartInfo.RedirectStandardError = true;
            //不显示程序窗口
            p1.StartInfo.CreateNoWindow = true;
            //应该触发退出事件
            p1.EnableRaisingEvents = true;

            string cmdstr0 = @"shell  echo 0x39 0x01 0x00 0x00 0x00 0x00 " + length.ToString() + " ";
            string cmdstr2 = " > /d/dri/0/DSI-1/tx_cmd";

            string cmdstr1 = "";
            for (int i = 0; i < length; i++)
            {
                cmdstr1 = cmdstr1 + data[i] + " ";
            }
            //注意最后有一个空格
            p1.StartInfo.Arguments = cmdstr0 + cmdstr1 + cmdstr2;
            p1.Start();
            //p1.WaitForExit(); //等待程序执行完退出进程
            //Thread.Sleep(5);
            p1.Close();
        }
        private void T20_Tick(object sender, EventArgs e)
        {
            RM692EX_AUTO_gamma(null, null);
        }

        //Gamma自动载入
        private void Multi_LoadGamma(string path)
        {
            string strtmp = "";
            //读取文本信息
            FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
            StreamReader streamReader = new StreamReader(fs, Encoding.GetEncoding("GBK"));
            streamReader.BaseStream.Seek(0, SeekOrigin.Begin);
            string strLine = streamReader.ReadLine();
            //循环读取文本信息
            while (!string.IsNullOrEmpty(strLine))
            {
                strtmp = strtmp + strLine + ",";
                strLine = streamReader.ReadLine(); //循环读取下一行
            }
            fs.Close(); //关闭流
            streamReader.Close();
            string[] arrtmp = strtmp.Split(new Char[] { ',' });
            RM692EX_R0.Text = arrtmp[1].ToString(); RM692EX_G0.Text = arrtmp[3].ToString(); RM692EX_B0.Text = arrtmp[5].ToString();
            RM692EX_R1.Text = arrtmp[7].ToString(); RM692EX_G1.Text = arrtmp[9].ToString(); RM692EX_B1.Text = arrtmp[11].ToString();
            RM692EX_R2.Text = arrtmp[13].ToString(); RM692EX_G2.Text = arrtmp[15].ToString(); RM692EX_B2.Text = arrtmp[17].ToString();
            RM692EX_R3.Text = arrtmp[19].ToString(); RM692EX_G3.Text = arrtmp[21].ToString(); RM692EX_B3.Text = arrtmp[23].ToString();
            RM692EX_R4.Text = arrtmp[25].ToString(); RM692EX_G4.Text = arrtmp[27].ToString(); RM692EX_B4.Text = arrtmp[29].ToString();
            RM692EX_R5.Text = arrtmp[31].ToString(); RM692EX_G5.Text = arrtmp[33].ToString(); RM692EX_B5.Text = arrtmp[35].ToString();
            RM692EX_R6.Text = arrtmp[37].ToString(); RM692EX_G6.Text = arrtmp[39].ToString(); RM692EX_B6.Text = arrtmp[41].ToString();
            RM692EX_R7.Text = arrtmp[43].ToString(); RM692EX_G7.Text = arrtmp[45].ToString(); RM692EX_B7.Text = arrtmp[47].ToString();
            RM692EX_R8.Text = arrtmp[49].ToString(); RM692EX_G8.Text = arrtmp[51].ToString(); RM692EX_B8.Text = arrtmp[53].ToString();
            RM692EX_R9.Text = arrtmp[55].ToString(); RM692EX_G9.Text = arrtmp[57].ToString(); RM692EX_B9.Text = arrtmp[59].ToString();
            RM692EX_R10.Text = arrtmp[61].ToString(); RM692EX_G10.Text = arrtmp[63].ToString(); RM692EX_B10.Text = arrtmp[65].ToString();
            RM692EX_R11.Text = arrtmp[67].ToString(); RM692EX_G11.Text = arrtmp[69].ToString(); RM692EX_B11.Text = arrtmp[71].ToString();
            RM692EX_R12.Text = arrtmp[73].ToString(); RM692EX_G12.Text = arrtmp[75].ToString(); RM692EX_B12.Text = arrtmp[77].ToString();
            RM692EX_R13.Text = arrtmp[79].ToString(); RM692EX_G13.Text = arrtmp[81].ToString(); RM692EX_B13.Text = arrtmp[83].ToString();
            RM692EX_R14.Text = arrtmp[85].ToString(); RM692EX_G14.Text = arrtmp[87].ToString(); RM692EX_B14.Text = arrtmp[89].ToString();
            RM692EX_R15.Text = arrtmp[91].ToString(); RM692EX_G15.Text = arrtmp[93].ToString(); RM692EX_B15.Text = arrtmp[95].ToString();
            RM692EX_R16.Text = arrtmp[97].ToString(); RM692EX_G16.Text = arrtmp[99].ToString(); RM692EX_B16.Text = arrtmp[101].ToString();
            RM692EX_R17.Text = arrtmp[103].ToString(); RM692EX_G17.Text = arrtmp[105].ToString(); RM692EX_B17.Text = arrtmp[107].ToString();
            RM692EX_R18.Text = arrtmp[109].ToString(); RM692EX_G18.Text = arrtmp[111].ToString(); RM692EX_B18.Text = arrtmp[113].ToString();
            RM692EX_R19.Text = arrtmp[115].ToString(); RM692EX_G19.Text = arrtmp[117].ToString(); RM692EX_B19.Text = arrtmp[119].ToString();
            RM692EX_R20.Text = arrtmp[121].ToString(); RM692EX_G20.Text = arrtmp[123].ToString(); RM692EX_B20.Text = arrtmp[125].ToString();
            RM692EX_R21.Text = arrtmp[127].ToString(); RM692EX_G21.Text = arrtmp[129].ToString(); RM692EX_B21.Text = arrtmp[131].ToString();
            RM692EX_R22.Text = arrtmp[133].ToString(); RM692EX_G22.Text = arrtmp[135].ToString(); RM692EX_B22.Text = arrtmp[137].ToString();
            RM692EX_R23.Text = arrtmp[139].ToString(); RM692EX_G23.Text = arrtmp[141].ToString(); RM692EX_B23.Text = arrtmp[143].ToString();
            RM692EX_R24.Text = arrtmp[145].ToString(); RM692EX_G24.Text = arrtmp[147].ToString(); RM692EX_B24.Text = arrtmp[149].ToString();
            RM692EX_R25.Text = arrtmp[151].ToString(); RM692EX_G25.Text = arrtmp[153].ToString(); RM692EX_B25.Text = arrtmp[155].ToString();
            RM692EX_R26.Text = arrtmp[157].ToString(); RM692EX_G26.Text = arrtmp[159].ToString(); RM692EX_B26.Text = arrtmp[161].ToString();
        }
        private void RM692EX_AUTO_gamma_stop(object sender, EventArgs e)
        {
            T20.Enabled = false;
        }
        private void btn_RM692Ex_Calculate_Click(object sender, EventArgs e)
        {
            // double Lr_d, Lg_d, Lb_d;
            double L_X, L_Y, L_Z;
            string[] arr = RM692EX_Gamma_SetBox.Lines;
            {
                try
                {
                    Diven_Gamma.Clear();
                    Diven_Offset.Clear();
                    for (int i = 0; i < arr.Length; i++)
                    {
                        string line = arr[i];
                        line = line.Replace(" ", "");
                        if (line.Contains("TUNING_SPEC"))
                        {
                            line = line.Replace("TUNING_SPEC", "");
                            line = line.Replace("(", "");
                            line = line.Replace(")", "");
                            string[] arr0 = line.Split(',');
                            Setting.Gamma_Property Gamma = new Setting.Gamma_Property(Convert.ToInt32(arr0[0]), Convert.ToDouble(arr0[1]), Convert.ToDouble(arr0[2]), Convert.ToDouble(arr0[3]), Convert.ToDouble(arr0[4]));
                            Diven_Gamma.Add(Gamma);
                        }
                        else if (line.Contains("LXY_SPEC"))
                        {
                            line = line.Replace("LXY_SPEC", "");
                            line = line.Replace("(", "");
                            line = line.Replace(")", "");
                            string[] arr1 = line.Split(',');
                            //将Offset对象放入集合
                            Setting.Offset_Property Offset = new Setting.Offset_Property(Convert.ToInt32(arr1[0]), Convert.ToDouble(arr1[1]), Convert.ToDouble(arr1[2]), Convert.ToDouble(arr1[3]));
                            Diven_Offset.Add(Offset);
                        }
                    }
                }
                catch
                {
                    MessageBox.Show("格式输入有误");
                }
            }
            //----------------------------------------------------------------------------
            try
            {
                txt2EX_lumoff.Text = Diven_Offset[0].LUM_OFF.ToString();
                txt2EX_Xoff.Text = Diven_Offset[0].X_OFF.ToString();
                txt2EX_Yoff.Text = Diven_Offset[0].Y_OFF.ToString();

                int Total_points = Diven_Gamma[0].Points_num;
                D_lums = new double[Total_points, 3];
                D_lum = new double[Total_points];
                txt2EX_Gamma.Text = Diven_Gamma[0].Curve.ToString();
                txt2EX_lum.Text = Diven_Gamma[0].Lum.ToString();
                txt2EX_X.Text = Diven_Gamma[0].X.ToString();
                txt2EX_Y.Text = Diven_Gamma[0].Y.ToString();
                Points_LV.Text = "";

                for (int i = 0; i < Total_points; i++)
                {   //Diven_Offset[p].NO 灰阶绑点       

                    D_lum[i] = (Math.Pow((Diven_Offset[i].NO / 255.0), Diven_Gamma[0].Curve) * Diven_Gamma[0].Lum); //每绑点目标亮度
                    Points_LV.Text = Points_LV.Text + Diven_Offset[i].NO + ":  " + Math.Round(D_lum[i], 6) + "\r\n";
                    CaSDKObj.Lvxy_to_XYZ(D_lum[i], Diven_Gamma[0].X, Diven_Gamma[0].Y, out L_X, out L_Y, out L_Z);//X/Z/Z转换
                    D_lums[i, 0] = L_X; //R/B/G分亮度计算
                    D_lums[i, 1] = L_Y;
                    D_lums[i, 2] = L_Z;
                }
            }
            catch
            {
                MessageBox.Show("格式输入有误");
            }
        }

        private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (RM692EX_Gamma_SetBox.Text == string.Empty)
            {
                MessageBox.Show("File Empty!");
            }
            else
            {
                //创建保存对话框对象
                SaveFileDialog save = new SaveFileDialog();
                save.Filter = "gamma cfg files (*.txt)|*.txt"; //过滤文件类型
                save.FilterIndex = 1;
                save.RestoreDirectory = true;
                if (save.ShowDialog() == DialogResult.OK)
                {
                    string path = save.FileName;
                    save.DefaultExt = ".txt";
                    StreamWriter sw = new StreamWriter(path);
                    sw.WriteLine(RM692EX_Gamma_SetBox.Text);
                    sw.Close();
                }
            }
        }
        private void Load_Gamma_Tuning_Load(object sender, EventArgs e)
        {
            RM692EX_Gamma_SetBox.Clear();
            //读取文本信息
            FileStream fs = new FileStream(Application.StartupPath + @"\l_gamma\NGamma.txt", FileMode.Open, FileAccess.Read);
            StreamReader streamReader = new StreamReader(fs, Encoding.GetEncoding("GBK"));
            streamReader.BaseStream.Seek(0, SeekOrigin.Begin);
            string str_generate = streamReader.ReadLine();
            //循环读取文本信息
            while (str_generate != null)
            {
                RM692EX_Gamma_SetBox.AppendText(str_generate + "\r\n");
                str_generate = streamReader.ReadLine(); //循环读取下一行
            }
            fs.Close(); //关闭流
            streamReader.Close();
        }

        private void 载入ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog of = new OpenFileDialog();
            of.Filter = "精度配置文件(*.txt)|*.txt|所有文件(*.*)|*.*";
            of.Title = "gamma cfg write";

            if (of.ShowDialog() == DialogResult.OK)
            {
                RM692EX_Gamma_SetBox.Clear();
                //读取文本信息
                FileStream fs = new FileStream(of.FileName, FileMode.Open, FileAccess.Read);
                StreamReader streamReader = new StreamReader(fs, Encoding.GetEncoding("GBK"));
                streamReader.BaseStream.Seek(0, SeekOrigin.Begin);
                string str_generate = streamReader.ReadLine();
                //循环读取文本信息
                while (str_generate != null)
                {
                    RM692EX_Gamma_SetBox.AppendText(str_generate + "\r\n");
                    str_generate = streamReader.ReadLine(); //循环读取下一行
                }
                fs.Close(); //关闭流
                streamReader.Close();
            }
        }



        private void RM692EX_Inter_Gamma_Click(object sender, EventArgs e)
        {
            byte[] gamma_reg = new byte[2] { 0xfe, 0x22 };
            ADB_info_Traffic(gamma_reg, 2);
            gamma_reg = new byte[2] { 0x5f, 0x02 };//Disable Gamma int
            ADB_info_Traffic(gamma_reg, 2);

            gamma_reg = new byte[2] { 0xfe, 0x70 };
            ADB_info_Traffic(gamma_reg, 2);
            gamma_reg = new byte[2] { 0x28, 0x00 };//Disable Gamma int
            ADB_info_Traffic(gamma_reg, 2);

            gamma_reg = new byte[2] { 0xfe, 0x40 };
            ADB_info_Traffic(gamma_reg, 2);

            gamma_reg = new byte[2] { 0xDA, 0xC8 };
            ADB_info_Traffic(gamma_reg, 2);

            gamma_reg = new byte[2] { 0xD7, byte.Parse(Gamma_Band_Select.Text) };
            ADB_info_Traffic(gamma_reg, 2);
            //----------------------------------------------
            gamma_reg = new byte[2] { 0xFE, 0x78 };
            ADB_info_Traffic(gamma_reg, 2);

            gamma_reg = new byte[2] { 0xF4, 0xC0 };
            ADB_info_Traffic(gamma_reg, 2);

            gamma_reg = new byte[2] { 0xF5, byte.Parse(Gamma_Band_Select.Text) };
            ADB_info_Traffic(gamma_reg, 2);

            gamma_reg = new byte[2] { 0xfe, 0x50 };
            ADB_info_Traffic(gamma_reg, 2);

        }

        private void button_bist_Click(object sender, EventArgs e)
        {
            Bist_pattern((byte)(btn_Bist_R.Value), (byte)(btn_Bist_G.Value), (byte)(btn_Bist_B.Value));
        }

        private void RM692EX_Exit_Gamma_Click(object sender, EventArgs e)
        {
            byte[] gamma_reg = new byte[2] { 0xfe, 0x22 };
            ADB_info_Traffic(gamma_reg, 2);
            gamma_reg = new byte[2] { 0x5f, 0x00 };//Disable Gamma int
            ADB_info_Traffic(gamma_reg, 2);
            gamma_reg = new byte[2] { 0xfe, 0x40 };
            ADB_info_Traffic(gamma_reg, 2);
            gamma_reg = new byte[2] { 0xDA, 0x88 };//Disable Gamma int
            ADB_info_Traffic(gamma_reg, 2);
            gamma_reg = new byte[2] { 0xfe, 0x78 };
            ADB_info_Traffic(gamma_reg, 2);
            gamma_reg = new byte[2] { 0xF4, 0x80 };
            ADB_info_Traffic(gamma_reg, 2);
            //----------------------------------------------
            gamma_reg = new byte[2] { 0xFE, 0x70 };
            ADB_info_Traffic(gamma_reg, 2);
            gamma_reg = new byte[2] { 0x28, 0x01 };
            ADB_info_Traffic(gamma_reg, 2);

        }


        private void Bist_pattern(byte RR, byte GG, byte BB)
        {
            byte[] gamma_reg = new byte[2] { 0xfe, 0x00 };
            ADB_info_Traffic(gamma_reg, 2);
            gamma_reg = new byte[2] { 0xfa, 0x07 };//Disable Gamma int
            ADB_info_Traffic(gamma_reg, 2);
            gamma_reg = new byte[2] { 0xfe, 0xd0 };
            ADB_info_Traffic(gamma_reg, 2);
            gamma_reg = new byte[2] { 0x3a, 0xc8 };//Disable Gamma int
            ADB_info_Traffic(gamma_reg, 2);
            gamma_reg = new byte[2] { 0x43, 0x80 };
            ADB_info_Traffic(gamma_reg, 2);
            gamma_reg = new byte[2] { 0x44, 0x00 };
            ADB_info_Traffic(gamma_reg, 2);
            gamma_reg = new byte[2] { 0x45, 0x21 };
            ADB_info_Traffic(gamma_reg, 2);
            gamma_reg = new byte[2] { 0x3b, RR };
            ADB_info_Traffic(gamma_reg, 2);
            gamma_reg = new byte[2] { 0x3d, GG };
            ADB_info_Traffic(gamma_reg, 2);
            gamma_reg = new byte[2] { 0x3f, BB };
            ADB_info_Traffic(gamma_reg, 2);
        }

        private void RM692EX_Gamma_Map(int index)
        {
            R_HZ[index, 16] = (int)(RM692EX_R16.Value);
            R_HZ[index, 15] = (int)(RM692EX_R15.Value);
            R_HZ[index, 14] = (int)(RM692EX_R14.Value);
            R_HZ[index, 13] = (int)(RM692EX_R13.Value);
            R_HZ[index, 12] = (int)(RM692EX_R12.Value);
            R_HZ[index, 11] = (int)(RM692EX_R11.Value);
            R_HZ[index, 10] = (int)(RM692EX_R10.Value);
            R_HZ[index, 9] = (int)(RM692EX_R9.Value);
            R_HZ[index, 8] = (int)(RM692EX_R8.Value);
            R_HZ[index, 7] = (int)(RM692EX_R7.Value);
            R_HZ[index, 6] = (int)(RM692EX_R6.Value);
            R_HZ[index, 5] = (int)(RM692EX_R5.Value);
            R_HZ[index, 4] = (int)(RM692EX_R4.Value);
            R_HZ[index, 3] = (int)(RM692EX_R3.Value);
            R_HZ[index, 2] = (int)(RM692EX_R2.Value);
            R_HZ[index, 1] = (int)(RM692EX_R1.Value);
            R_HZ[index, 0] = (int)(RM692EX_R0.Value);

            G_HZ[index, 16] = (int)(RM692EX_G16.Value);
            G_HZ[index, 15] = (int)(RM692EX_G15.Value);
            G_HZ[index, 14] = (int)(RM692EX_G14.Value);
            G_HZ[index, 13] = (int)(RM692EX_G13.Value);
            G_HZ[index, 12] = (int)(RM692EX_G12.Value);
            G_HZ[index, 11] = (int)(RM692EX_G11.Value);
            G_HZ[index, 10] = (int)(RM692EX_G10.Value);
            G_HZ[index, 9] = (int)(RM692EX_G9.Value);
            G_HZ[index, 8] = (int)(RM692EX_G8.Value);
            G_HZ[index, 7] = (int)(RM692EX_G7.Value);
            G_HZ[index, 6] = (int)(RM692EX_G6.Value);
            G_HZ[index, 5] = (int)(RM692EX_G5.Value);
            G_HZ[index, 4] = (int)(RM692EX_G4.Value);
            G_HZ[index, 3] = (int)(RM692EX_G3.Value);
            G_HZ[index, 2] = (int)(RM692EX_G2.Value);
            G_HZ[index, 1] = (int)(RM692EX_G1.Value);
            G_HZ[index, 0] = (int)(RM692EX_G0.Value);

            B_HZ[index, 16] = (int)(RM692EX_B16.Value);
            B_HZ[index, 15] = (int)(RM692EX_B15.Value);
            B_HZ[index, 14] = (int)(RM692EX_B14.Value);
            B_HZ[index, 13] = (int)(RM692EX_B13.Value);
            B_HZ[index, 12] = (int)(RM692EX_B12.Value);
            B_HZ[index, 11] = (int)(RM692EX_B11.Value);
            B_HZ[index, 10] = (int)(RM692EX_B10.Value);
            B_HZ[index, 9] = (int)(RM692EX_B9.Value);
            B_HZ[index, 8] = (int)(RM692EX_B8.Value);
            B_HZ[index, 7] = (int)(RM692EX_B7.Value);
            B_HZ[index, 6] = (int)(RM692EX_B6.Value);
            B_HZ[index, 5] = (int)(RM692EX_B5.Value);
            B_HZ[index, 4] = (int)(RM692EX_B4.Value);
            B_HZ[index, 3] = (int)(RM692EX_B3.Value);
            B_HZ[index, 2] = (int)(RM692EX_B2.Value);
            B_HZ[index, 1] = (int)(RM692EX_B1.Value);
            B_HZ[index, 0] = (int)(RM692EX_B0.Value);
        }
        private void RM692EX_Calu_90HZ_Gamma()
        {
            //60HZ：1-10，90HZ：11-20，120HZ：21-30
            //需返工 1-5；11-15；21-25
            for (int j = 1; j < 6; j++)
            {
                for (int i = 0; i < 17; i++)
                {
                    R_HZ[10 + j, i] = (R_HZ[j, i] + R_HZ[20 + j, i]) / 2;
                    G_HZ[10 + j, i] = (G_HZ[j, i] + G_HZ[20 + j, i]) / 2;
                    B_HZ[10 + j, i] = (B_HZ[j, i] + B_HZ[20 + j, i]) / 2;
                }
            }
        }

        private void btn_RM692EX_0Cal_Click(object sender, EventArgs e)
        {
            frmHelp.btnCal_Click(null, null);
        }

        private void RM692E5_ADB_Load(object sender, EventArgs e)
        {
            Load_Gamma_Tuning_Load(null, null);
        }
        private void RM692EX_Bist_OFF(object sender, EventArgs e)
        {
            byte[] gamma_reg = new byte[2] { 0xfe, 0xD0 };
            ADB_info_Traffic(gamma_reg, 2);
            gamma_reg = new byte[2] { 0x3A, 0x4C }; //
            ADB_info_Traffic(gamma_reg, 2);
        }

        private void RM692EX_120HZ_Gamma_Write(object sender, EventArgs e)
        {
            byte[] gamma_reg = new byte[2] { 0xfe, 0x40 };
            ADB_info_Traffic(gamma_reg, 2);
            //gamma_reg = new byte[2] { 0xBD, 0x00 }; //60HZ
            gamma_reg = new byte[2] { 0xBD, 0x07 }; //120HZ
            ADB_info_Traffic(gamma_reg, 2);

            gamma_reg = new byte[2] { 0xfe, 0x50 };
            ADB_info_Traffic(gamma_reg, 2);
            gamma_reg = new byte[2] { 0xE5, 0x11 };
            ADB_info_Traffic(gamma_reg, 2);
            gamma_reg = new byte[2] { 0xE6, 0xC0 };
            ADB_info_Traffic(gamma_reg, 2);
            gamma_reg = new byte[2] { 0xE7, 0xC0 };
            ADB_info_Traffic(gamma_reg, 2);

            int m = 0;
            for (int j = 1; j < 6; j++)
            {
                Gamma_Band_Select.Text = (j + 20).ToString(); //90HZ band 11~15
                Application.DoEvents();

                gamma_reg = new byte[2] { 0xfe, 0x70 };
                ADB_info_Traffic(gamma_reg, 2);
                gamma_reg = new byte[2] { 0x28, 0x00 };//Disable Gamma int
                ADB_info_Traffic(gamma_reg, 2);

                gamma_reg = new byte[2] { 0xfe, 0x40 };
                ADB_info_Traffic(gamma_reg, 2);

                gamma_reg = new byte[2] { 0xDA, 0xC8 };
                ADB_info_Traffic(gamma_reg, 2);

                gamma_reg = new byte[2] { 0xD7, byte.Parse(Gamma_Band_Select.Text) };
                ADB_info_Traffic(gamma_reg, 2);
                //----------------------------------------------
                gamma_reg = new byte[2] { 0xFE, 0x78 };
                ADB_info_Traffic(gamma_reg, 2);

                gamma_reg = new byte[2] { 0xF4, 0xC0 };
                ADB_info_Traffic(gamma_reg, 2);

                gamma_reg = new byte[2] { 0xF5, byte.Parse(Gamma_Band_Select.Text) };
                ADB_info_Traffic(gamma_reg, 2);

                gamma_reg = new byte[2] { 0xfe, 0x50 };
                ADB_info_Traffic(gamma_reg, 2);

                m = 0;
                for (int i = 0; i < 17; i++)
                {
                    gamma_reg = new byte[2] { gamma_r[m], (byte)((R_HZ[20 + j, i] >> 8) & 0x0f) };
                    ADB_info_Traffic(gamma_reg, 2);
                    gamma_reg = new byte[2] { gamma_r[m + 1], (byte)(R_HZ[20 + j, i] & 0xff) };
                    ADB_info_Traffic(gamma_reg, 2);

                    gamma_reg = new byte[2] { gamma_g[m], (byte)((G_HZ[20 + j, i] >> 8) & 0x0f) };
                    ADB_info_Traffic(gamma_reg, 2);
                    gamma_reg = new byte[2] { gamma_g[m + 1], (byte)(G_HZ[20 + j, i] & 0xff) };
                    ADB_info_Traffic(gamma_reg, 2);

                    gamma_reg = new byte[2] { gamma_b[m], (byte)((B_HZ[20 + j, i] >> 8) & 0x0f) };
                    ADB_info_Traffic(gamma_reg, 2);
                    gamma_reg = new byte[2] { gamma_b[m + 1], (byte)(B_HZ[20 + j, i] & 0xff) };
                    ADB_info_Traffic(gamma_reg, 2);

                    m = m + 2;
                    //Thread.Sleep(15);
                }
            }
            //MessageBox.Show("120HZ 载入完成！");
            textBox1.Text = textBox1.Text + "120HZ Data Write finish!\r\n";
        }

        private void RM692EX_60HZ_Gamma_Write(object sender, EventArgs e)
        {
            byte[] gamma_reg = new byte[2] { 0xfe, 0x40 };
            ADB_info_Traffic(gamma_reg, 2);
            gamma_reg = new byte[2] { 0xBD, 0x00 }; //60HZ
            //gamma_reg = new byte[2] { 0xBD, 0x07 }; //120HZ
            ADB_info_Traffic(gamma_reg, 2);

            gamma_reg = new byte[2] { 0xfe, 0x50 };
            ADB_info_Traffic(gamma_reg, 2);
            gamma_reg = new byte[2] { 0xE5, 0x11 };
            ADB_info_Traffic(gamma_reg, 2);
            gamma_reg = new byte[2] { 0xE6, 0xC0 };
            ADB_info_Traffic(gamma_reg, 2);
            gamma_reg = new byte[2] { 0xE7, 0xC0 };
            ADB_info_Traffic(gamma_reg, 2);

            int m = 0;
            for (int j = 1; j < 6; j++)
            {
                Gamma_Band_Select.Text = (j + 0).ToString(); //90HZ band 11~15
                Application.DoEvents();

                gamma_reg = new byte[2] { 0xfe, 0x70 };
                ADB_info_Traffic(gamma_reg, 2);
                gamma_reg = new byte[2] { 0x28, 0x00 };//Disable Gamma int
                ADB_info_Traffic(gamma_reg, 2);

                gamma_reg = new byte[2] { 0xfe, 0x40 };
                ADB_info_Traffic(gamma_reg, 2);

                gamma_reg = new byte[2] { 0xDA, 0xC8 };
                ADB_info_Traffic(gamma_reg, 2);

                gamma_reg = new byte[2] { 0xD7, byte.Parse(Gamma_Band_Select.Text) };
                ADB_info_Traffic(gamma_reg, 2);
                //----------------------------------------------
                gamma_reg = new byte[2] { 0xFE, 0x78 };
                ADB_info_Traffic(gamma_reg, 2);

                gamma_reg = new byte[2] { 0xF4, 0xC0 };
                ADB_info_Traffic(gamma_reg, 2);

                gamma_reg = new byte[2] { 0xF5, byte.Parse(Gamma_Band_Select.Text) };
                ADB_info_Traffic(gamma_reg, 2);

                gamma_reg = new byte[2] { 0xfe, 0x50 };
                ADB_info_Traffic(gamma_reg, 2);

                m = 0;
                for (int i = 0; i < 17; i++)
                {
                    gamma_reg = new byte[2] { gamma_r[m], (byte)((R_HZ[0 + j, i] >> 8) & 0x0f) };
                    ADB_info_Traffic(gamma_reg, 2);
                    gamma_reg = new byte[2] { gamma_r[m + 1], (byte)(R_HZ[0 + j, i] & 0xff) };
                    ADB_info_Traffic(gamma_reg, 2);

                    gamma_reg = new byte[2] { gamma_g[m], (byte)((G_HZ[0 + j, i] >> 8) & 0x0f) };
                    ADB_info_Traffic(gamma_reg, 2);
                    gamma_reg = new byte[2] { gamma_g[m + 1], (byte)(G_HZ[0 + j, i] & 0xff) };
                    ADB_info_Traffic(gamma_reg, 2);

                    gamma_reg = new byte[2] { gamma_b[m], (byte)((B_HZ[0 + j, i] >> 8) & 0x0f) };
                    ADB_info_Traffic(gamma_reg, 2);
                    gamma_reg = new byte[2] { gamma_b[m + 1], (byte)(B_HZ[0 + j, i] & 0xff) };
                    ADB_info_Traffic(gamma_reg, 2);

                    m = m + 2;
                    //Thread.Sleep(15);
                }
            }
            //MessageBox.Show("60HZ 载入完成！");
            textBox1.Text = textBox1.Text + "60HZ Data Write finish!\r\n";
        }
        private int gamma_group_use_flag = 0;
        private void button3_Click(object sender, EventArgs e)
        {
            if (gamma_group_use_flag == 0)
            {
                gamma_group_use_flag = 1;
                groupBox1.Enabled = true;
                groupBox142.Enabled = true;
                groupBox143.Enabled = true;
                groupBox144.Enabled = true;
                groupBox147.Enabled = true;
            }
            else if (gamma_group_use_flag == 1)
            {
                gamma_group_use_flag = 0;
                groupBox1.Enabled = false;
                groupBox142.Enabled = false;
                groupBox143.Enabled = false;
                groupBox144.Enabled = false;
                groupBox147.Enabled = false;
            }
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            textBox1.SelectionStart = textBox1.Text.Length; //Set the current caret position at the end
            textBox1.ScrollToCaret(); //Now scroll it automatically
        }

        private void RM692EX_90HZ_Gamma_Write(object sender, EventArgs e)
        {
            byte[] gamma_reg = new byte[2] { 0xfe, 0x40 };
            ADB_info_Traffic(gamma_reg, 2);
            //gamma_reg = new byte[2] { 0xBD, 0x00 }; //60HZ
            //gamma_reg = new byte[2] { 0xBD, 0x07 }; //120HZ
            gamma_reg = new byte[2] { 0xBD, 0x06 }; //90HZ
            ADB_info_Traffic(gamma_reg, 2);
            gamma_reg = new byte[2] { 0xfe, 0x50 };
            ADB_info_Traffic(gamma_reg, 2);
            gamma_reg = new byte[2] { 0xE5, 0x11 };
            ADB_info_Traffic(gamma_reg, 2);
            gamma_reg = new byte[2] { 0xE6, 0xC0 };
            ADB_info_Traffic(gamma_reg, 2);
            gamma_reg = new byte[2] { 0xE7, 0xC0 };
            ADB_info_Traffic(gamma_reg, 2);

            int m = 0;
            for (int j = 1; j < 6; j++)
            {
                Gamma_Band_Select.Text = (j + 10).ToString(); //90HZ band 11~15
                Application.DoEvents();

                gamma_reg = new byte[2] { 0xfe, 0x22 };
                ADB_info_Traffic(gamma_reg, 2);
                gamma_reg = new byte[2] { 0x5f, 0x02 };//Disable Gamma int
                ADB_info_Traffic(gamma_reg, 2);

                gamma_reg = new byte[2] { 0xfe, 0x70 };
                ADB_info_Traffic(gamma_reg, 2);
                gamma_reg = new byte[2] { 0x28, 0x00 };//Disable Gamma int
                ADB_info_Traffic(gamma_reg, 2);

                gamma_reg = new byte[2] { 0xfe, 0x40 };
                ADB_info_Traffic(gamma_reg, 2);

                gamma_reg = new byte[2] { 0xDA, 0xC8 };
                ADB_info_Traffic(gamma_reg, 2);

                gamma_reg = new byte[2] { 0xD7, byte.Parse(Gamma_Band_Select.Text) };
                ADB_info_Traffic(gamma_reg, 2);
                //----------------------------------------------
                gamma_reg = new byte[2] { 0xFE, 0x78 };
                ADB_info_Traffic(gamma_reg, 2);

                gamma_reg = new byte[2] { 0xF4, 0xC0 };
                ADB_info_Traffic(gamma_reg, 2);

                gamma_reg = new byte[2] { 0xF5, byte.Parse(Gamma_Band_Select.Text) };
                ADB_info_Traffic(gamma_reg, 2);

                gamma_reg = new byte[2] { 0xfe, 0x50 };
                ADB_info_Traffic(gamma_reg, 2);



                // RM692EX_Inter_Gamma_Click(null, null);
                //Thread.Sleep(15);
                m = 0;
                for (int i = 0; i < 17; i++)
                {
                    gamma_reg = new byte[2] { gamma_r[m], (byte)((R_HZ[10 + j, i] >> 8) & 0x0f) };
                    ADB_info_Traffic(gamma_reg, 2);
                    gamma_reg = new byte[2] { gamma_r[m + 1], (byte)(R_HZ[10 + j, i] & 0xff) };
                    ADB_info_Traffic(gamma_reg, 2);

                    gamma_reg = new byte[2] { gamma_g[m], (byte)((G_HZ[10 + j, i] >> 8) & 0x0f) };
                    ADB_info_Traffic(gamma_reg, 2);
                    gamma_reg = new byte[2] { gamma_g[m + 1], (byte)(G_HZ[10 + j, i] & 0xff) };
                    ADB_info_Traffic(gamma_reg, 2);

                    gamma_reg = new byte[2] { gamma_b[m], (byte)((B_HZ[10 + j, i] >> 8) & 0x0f) };
                    ADB_info_Traffic(gamma_reg, 2);
                    gamma_reg = new byte[2] { gamma_b[m + 1], (byte)(B_HZ[10 + j, i] & 0xff) };
                    ADB_info_Traffic(gamma_reg, 2);

                    m = m + 2;
                    //Thread.Sleep(15);
                }
            }
            //MessageBox.Show("90HZ 载入完成！");
            textBox1.Text = textBox1.Text + "90HZ Data Write finish!\r\n";
        }
        private void RM692EX_Gamma_OTP(object sender, EventArgs e)
        {

            ADB_Inter_root();//重新root
            // RM692EX_Exit_Gamma_Click(null,null);
            byte[] gamma_reg = new byte[2] { 0xfe, 0xD0 };
            ADB_info_Traffic(gamma_reg, 2);
            gamma_reg = new byte[2] { 0x3A, 0x4C };
            ADB_info_Traffic(gamma_reg, 2);
            Thread.Sleep(500);
            gamma_reg = new byte[2] { 0xfe, 0x20 };
            ADB_info_Traffic(gamma_reg, 2);
            gamma_reg = new byte[2] { 0x00, 0x00 };
            ADB_info_Traffic(gamma_reg, 2);
            gamma_reg = new byte[2] { 0xfe, 0x00 };
            ADB_info_Traffic(gamma_reg, 2);
            gamma_reg = new byte[1] { 0x28 };
            ADB_info_Traffic(gamma_reg, 1);
            Thread.Sleep(500);
            //gamma_reg = new byte[1] { 0x10 };
            //ADB_info_Traffic(gamma_reg, 1);
            //Thread.Sleep(250);
            gamma_reg = new byte[2] { 0xFE, 0xAA };
            ADB_info_Traffic(gamma_reg, 2);
            gamma_reg = new byte[2] { 0x79, 0x08 }; //Page26(AVDD)2nd enable
            ADB_info_Traffic(gamma_reg, 2);

            gamma_reg = new byte[2] { 0xFE, 0xAB };
            ADB_info_Traffic(gamma_reg, 2);
            gamma_reg = new byte[2] { 0x42, 0x02 }; //DYNPWR 2ND ENABLE
            ADB_info_Traffic(gamma_reg, 2);
            gamma_reg = new byte[2] { 0xFE, 0xAC };
            ADB_info_Traffic(gamma_reg, 2);
            gamma_reg = new byte[2] { 0xE0, 0xA8 }; //Gamma1~Gamma3 2nd enable
            ADB_info_Traffic(gamma_reg, 2);
            gamma_reg = new byte[2] { 0xE1, 0x0A }; //Gamma4~5 2nd enable
            ADB_info_Traffic(gamma_reg, 2);
            gamma_reg = new byte[2] { 0xE2, 0x80 }; //Gamma11 2nd enable
            ADB_info_Traffic(gamma_reg, 2);
            gamma_reg = new byte[2] { 0xE3, 0xAA }; //Gamma2~15 2nd enable
            ADB_info_Traffic(gamma_reg, 2);
            gamma_reg = new byte[2] { 0xE4, 0x00 };
            ADB_info_Traffic(gamma_reg, 2);

            gamma_reg = new byte[2] { 0xE5, 0xA8 }; //Gamma21~23 2nd enable
            ADB_info_Traffic(gamma_reg, 2);
            gamma_reg = new byte[2] { 0xE6, 0x0A };//Gamma24~25 2nd enable
            ADB_info_Traffic(gamma_reg, 2);

            gamma_reg = new byte[2] { 0xFE, 0xFF };
            ADB_info_Traffic(gamma_reg, 2);
            gamma_reg = new byte[2] { 0xF3, 0xA5 };
            ADB_info_Traffic(gamma_reg, 2);
            gamma_reg = new byte[2] { 0xF4, 0x5A };
            ADB_info_Traffic(gamma_reg, 2);
            gamma_reg = new byte[2] { 0xF5, 0x3C };
            ADB_info_Traffic(gamma_reg, 2);
            gamma_reg = new byte[2] { 0xD3, 0x00 }; //INTER POWER
            ADB_info_Traffic(gamma_reg, 2);
            gamma_reg = new byte[2] { 0xF2, 0x01 };
            ADB_info_Traffic(gamma_reg, 2);

            Thread.Sleep(3500);

            MessageBox.Show("OTP Complete!");
        }
    }

}
#endregion